package model

import (
	"git.oschina.net/ant180/scan/config"
	"github.com/go-redis/redis"
)

type Redis struct {
	conf   *config.RedisConf
	client *redis.Client
}

func (p *Redis) connect() {
	p.client = redis.NewClient(&redis.Options{
		Addr: p.conf.Addr,
		DB:   p.conf.DB,
	})
}

func NewRedis(conf *config.RedisConf) (*Redis, error) {
	//conf must set
	r := &Redis{
		conf: conf,
	}

	r.connect()

	return r, nil
}

func (p *Redis) ZAdd(setName, key string, score int64) error {
	return p.client.ZAdd(setName, redis.Z{float64(score), key}).Err()
}

func (p *Redis) LPop(key string) (string, error) {
	return p.client.LPop(key).Result()
}

func (p *Redis) LPush(key string, values ...interface{}) (int64, error) {
	return p.client.LPush(key, values...).Result()
}
